package com.dwj.auto.comment.common.wechat.handler;

import com.dwj.auto.comment.dao.entity.SubscribeUser;
import com.dwj.auto.comment.dao.entity.User;
import com.dwj.auto.comment.model.wechat.WxMessage;
import com.dwj.auto.comment.service.ISubscribeUserService;
import com.dwj.auto.comment.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName SubscribeListener
 * @Description 取消关注事件监听者
 * @Author dwjian
 * @Date 2020/12/26 19:25
 */
@Slf4j
@Component
public class NuSubscribeHandler extends WxEventMessageHandler implements WxMessageHandler {

    @Resource
    private IUserService userService;
    @Resource
    private ISubscribeUserService subscribeUserService;

    @Override
    public String dealMessage(WxMessage message) {
        SubscribeUser localSubUser = subscribeUserService.lambdaQuery().eq(SubscribeUser::getOpenId, message.getOpenId()).eq(SubscribeUser::getIsDel, 0).one();
        if(localSubUser != null){
            subscribeUserService.lambdaUpdate()
                    .eq(SubscribeUser::getId, localSubUser.getId())
                    .set(SubscribeUser::getUnSubDate, new Date())
                    .set(SubscribeUser::getStatus, 0).update();
        }
        if(localSubUser != null){
            //冻结账号
            userService.lambdaUpdate()
                    .eq(User::getId, localSubUser.getUserId()).eq(User::getIsDel, 0)
                    .set(User::getStatus, 1).update();
        }
        return null;
    }
}
